/*
 * program.c
 *
 *  Created on: 2018-1-23
 *      Author: root
 */
#include "program.h"
#include <unistd.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "ffs.h"
#include "app_debug_main.h"
#include "tools.h"
#include "protocol_meter.h"
#include "log_main.h"

#include "tcu_can.h"
#include <sys/msg.h>
#include "database.h"
#include "protocol_can.h"

#include "lib_time.h"
#include "myconfig.h"
#include "meter_com.h"
#ifdef NW_CLIENT
#include "meter_nw.h"
#include "pro_104.h"
#endif

#ifdef XJ_MQTT_CLIENT
#include "interface_xj.h"
#endif

#ifdef QL_CLOUD_CLIENT
#include "interface_ql.h"
#endif

#include "database.h"
#include "include.h"
chargpile_data *g_uidata = NULL;

//tran_record g_transaction;
//bus_data g_bus;


pconf conf;
pconf *pconfig=&conf;

char _tmp_print_str[128];
u8  g_ifApp[]={_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_,_IDEL_MODE_CARD_};

//粤易充 1.本地刷卡终止，2.远程终止充电，3充满自动停止，4余额不足，5充电桩故障，6 BMS故障，7、急停按钮停止，
//8.验证码停止，9.停电结束，10.按电量满足结束，11.按金额满足结束，12.按时间满足结束，13.拔枪结束，
char  g_stop_reason[]={0,0,0,0,0,0,0,0};

int   g_stop_detail[]={0,0,0,0,0,0,0,0};
u32 flag_config=0;
//Gun_Can_Info gun_can_info_tab[]={
//		{0,0,0xf6,0x8a},
//		{1,0,0xf6,0x8a},
////	{0,0,0xf6,0x8a},
////	{1,0,0xf6,0x8a},
////	{0,0,0xf6,0x8a},
////	{1,0,0xf6,0x8a},
////	{0,0,0xf6,0x8a},
////	{1,0,0xf6,0x8a},
//};
#ifdef XJ_MQTT_CLIENT
tran_record_xj  cur_record[GUN_NUM];
#endif

#ifdef NW_CLIENT
tran_record cur_record[GUN_NUM];
#endif

#ifdef QL_CLOUD_CLIENT
tran_record_ql  cur_record[GUN_NUM];
#endif

 int g_sss[]={0,0,0,0,0,0,0,0};
//u16 g_ser_num=0;
 u8 card_err_state[]={0,0,0,0,0,0,0,0};
u8 gm_sys_state[]={_SYS_READY_,_SYS_READY_,_SYS_READY_,_SYS_READY_,_SYS_READY_,_SYS_READY_,_SYS_READY_,_SYS_READY_,};
u8 gm_ctrl_state[]={_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,_CTRL_NORMAL_,};
u8 g_msg_save[]={_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,_MSG_NULL_,};
callbackFun main_updateYX=NULL;
callbackFun main_updateYC=NULL;
callbackFun main_updateMtData=NULL;
callbackFun main_updateMtDataReal=NULL;
callbackFun main_FinStartCharge=NULL;
callbackFun main_FinStopCharge=NULL;
int TCUStartChargeProcess(u8 gunid,u8 ctrltype)
{
	int ret = 1;
	int ret1;
	int ret_ql = 0;
//	if(gm_sys_state[gunid] !=_SYS_READY_)
//	{
//		return 7;
//	}
	int ret2;
	static start_mx start;
	static StartInfo ppp;//=NULL;//=(StartInfo *)pdata;
#ifdef NW_CLIENT
	ppp.p_chargepara=NULL;
	ret2=ifModelOp(gunid);

	if (ret2 == 0xff) {
		ui_debug("没有有效的计费模型！！！！");
		g_uidata->gunmsg[gunid].stopreason=11;
		usleep(10000);
		ret= INVALID_MODLE;
	}
	else
#endif
	{
	    if (ctrltype == _CTRL_PW_EN_
				&& (pconfig->ctrlpw_type != _CTRL_PW_EN_ || gunid != 0)) {
			ui_debug("不满足共充条件！！！！");
			ret = COMB_DISABLE;
			ret_ql = 0xFF;
		}else if (g_realData[gunid].failReadMt== 1) {
			ui_debug("电表通信错误！！！！");
			ret = MT_COMM_ERR;
			ret_ql = 0x3C;
		} else if (g_candata.errcode[gunid]) {
			ui_debug("can通信错误！！！！");
			ret = 400+(g_candata.errcode[gunid]-1);
			ret_ql = 0x02;
		} else if (g_uidata->gunmsg[gunid].gun_conn_state != 0) {
			ui_debug("枪未连接！！！！！");
			ret = GUN_DISCONN;
			ret_ql = 0xF1;
		} else if (getChargeState(gunid) != 0) //正在充电
				{
			ui_debug("桩正在充电！！！！！");
			ret =CHA_START_FIN;
			ret_ql = 0xF0;
		} else {

			ret1=protocol_can_start_charge(gunid,ctrltype);
			if (ret1 == 2) {
				ui_debug("不满足共充条件！！！！！！！！！");
				ret = COMB_DISABLE;
				ret_ql = 0xFF;
			} else if(ret1==0)
			   return 0;
			else
				ui_debug("桩未能准备好！！！！！");
				ret = TCU_NOT_READY;
				ret_ql = 0xF2;
			}
	}
	
#ifdef	 NW_CLIENT
    if (g_ifApp[gunid]==1 && ret>1) {
		start.MX1 = 0;
		start.MX2 = 1;

    	ppp.p_startInfo->MX3=ret;
        if(g_uidata->net_state==1)
        {
    		ui_debug("111APP充电，桩没有准备好充电启动结果传送到平台");

        	if(main_FinStartCharge!=NULL)
        	{
        		ui_debug("222APP充电，桩没有准备好充电启动结果传送到平台");
    	        main_FinStartCharge(gunid,(void*)&ppp);
        	}
        }

		printf("APP充电，桩没有准备好 ret=%d\r\n",ret);
	}

#endif

#ifdef QL_CLOUD_CLIENT

    if (g_ifApp[gunid]==1 && ret_ql >1) //g_ifApp[gunid]==1 _APP_MODE_
    {
    	start.MX1 = 0;
    	start.MX2 = 1;
    	ppp.p_startInfo = &start;
    	ppp.p_startInfo->MX3 = ret_ql;
    	if(g_uidata->net_state==1)
    	{
    		ui_debug("111APP充电，桩没有准备好充电启动结果传送到平台");
    		if(main_FinStartCharge!=NULL)
    		{
    			ui_debug("222APP充电，桩没有准备好充电启动结果传送到平台");
    			main_FinStartCharge(gunid,(void*)&ppp);
    		}
    	}
    	printf("APP充电，桩没有准备好 ret=%d\r\n",ret);
    }
#endif

     return ret;
}
int TCUStopChargeProcess(u8 gunid,u8 stopreason)
{
	int ret = 0;
	u8 charge_state;
	charge_state = getChargeState(gunid);
	ui_debug("桩正在停止充电！！！！！g_stop_reason[%d]=%d charge_state=%d  gm_sys_state[%d]=%d",
			  gunid, g_stop_reason[gunid], charge_state, gunid,gm_sys_state[gunid]);
	if (gm_sys_state[gunid] == _SYS_WAIT_CHARGING_START_)
		gm_sys_state[gunid] = _SYS_WAIT_STOP_1_;

	else
		gm_sys_state[gunid] = _SYS_WAIT_STOP_;

	g_sss[gunid] = 0;
	g_stop_reason[gunid] = stopreason;
    protocol_can_stop_charge(gunid, 1); //1-计费控制单元正常停止2-计费控制单元故障终止
	ui_debug("111桩正在停止充电！！！！！g_stop_reason[%d]=%d charge_state=%d", gunid,
			 g_stop_reason[gunid], charge_state);
	// 避免停止过程中生成充电费用，使装置和后台金额不一致 zhouhao 2021-07-14
	//g_uidata->gunmsg[gunid].charge_state = 4; //4 表示充电结束
	//g_uidata->cmd4[gunid].result=0;//发起充电时由UI写入0,由主控写入命令返回结果1成功2失败

	return ret;
}

int initUI(void)
{
//	char strip[32];
//	char *p1,*p2 ;
    printf("000wwwwwwwwwwwwwwwwwwwwfffkkk\r\n");
//	memcpy(strip,pconfig->server_ip,32);
	g_uidata->devtype = pconfig->devtype;
	g_uidata->update_state = 0;
	g_uidata->net_state = 0;
//	g_uidata->cmd1.flag_change=0;
 //   p1=strip;
//    printf("000wwwwwwwwwwwwwwwwwwwwfffkkk000 %s\r\n",p1);
//	p2=strstr(p1,".");
 //   printf("000wwwwwwwwwwwwwwwwwwwwfffkkk111\r\n");
 //   *p2='\0';
 //   g_uidata->cmd1.ip[3]= atoi(p1);
  //  printf("000wwwwwwwwwwwwwwwwwwwwfffkkk222\r\n");
 //   p1=p2+1;
//	p2=strstr(p1,".");

 //   *p2='\0';
//    g_uidata->cmd1.ip[2]= atoi(p1);
  //  printf("000wwwwwwwwwwwwwwwwwwwwfffkkk333\r\n");
 //   p1=p2+1;
//	p2=strstr(p1,".");
//    *p2='\0';
  //  g_uidata->cmd1.ip[1]= atoi(p1);

//    p1=p2+1;
 //   p2=p1+strlen(p1);
//	*p2='\0';
//	g_uidata->cmd1.ip[0]= atoi(p1);
//
//	g_uidata->cmd1.port=pconfig->server_port;
//	g_uidata->cmd1.MaxV=pconfig->MaxV;
//	g_uidata->cmd1.MaxI=pconfig->MaxI;
	g_uidata->update_state=0;

    printf("000wwwwwwwwwwwwwwwwwwwwfffkkk111\r\n");

//	memcpy(g_uidata->cmd1.propertyCode,pconfig->propertyCode,24);
//	memcpy(g_uidata->cmd1.pileCode,&pconfig->devNO[0],16);
//	main_debug("strip=%s,ip[0]=%d,ip[1]=%d,ip[2]=%d,ip[3]=%d port=%d",pconfig->server_ip,g_uidata->cmd1.ip[0]
//                            ,g_uidata->cmd1.ip[1],g_uidata->cmd1.ip[2],g_uidata->cmd1.ip[3],g_uidata->cmd1.port);
	g_uidata->gunnum=pconfig->gunnum;
//	main_debug("枪的个数 =%d  桩编号=%.*a",g_uidata->gunnum,16,g_uidata->cmd1.pileCode);

#ifdef XJ_MQTT_CLIENT
	int i;
	for (i = 0; i < pconfig->gunnum; i++) {

		g_uidata->can_state[i]=1;
		g_uidata->gunmsg[i].charge_state=0;
		g_uidata->gunmsg[i].gun_conn_state=0;
		g_uidata->cmd6[i].req_state = 1;
		g_uidata->cmd6[i].guntab = i;
		initQRCode(i); //0,g_uidata->cmd6[0].qrCode
	}
	meter_err_state[i]=0;
#endif

#ifdef QL_CLOUD_CLIENT
    int i;
    for (i = 0; i < pconfig->gunnum; i++) {

        g_uidata->can_state[i]=1;
        g_uidata->gunmsg[i].charge_state=0;
        g_uidata->gunmsg[i].gun_conn_state=0;
        g_uidata->cmd6[i].req_state = 1;
        g_uidata->cmd6[i].guntab = i;
        initQRCode(i); //0,g_uidata->cmd6[0].qrCode
    }
    meter_err_state[i]=0;
#endif

////	if(g_uidata->gunnum==2)
//	{
//		g_uidata->cmd6[1].req_state=1;
//		g_uidata->cmd6[1].guntab=1;
//		initQRCode(1,g_uidata->cmd6[1].qrCode);
//	}


    return 0;

}
int updateStatus2UI(u8 gunid,chargpile_data *ui)
{
	static char last_meter_state[]={0,0,0,0,0,0,0,0};
	static char last_can_state[]={0,0,0,0,0,0,0,0};
	static u8 last_charge_state[8];
    u8 charge_state[8];
    u8 err_code[]={0,0,0,0,0,0,0,0,0,0,0};
    u8 errcode;

    // CAN 通讯状态 
    if ((g_candata.errcode[gunid] & 1)>0)  //1心跳超时
    {
    	 ui->can_state[gunid] = 1;//g_candata.errcode[gunid];
    }
    else if((g_candata.errcode[gunid] & 2)>0) //2遥测超时
    {
   	    ui->can_state[gunid] = 2;//g_candata.errcode[gunid];
    }
    else if((g_candata.errcode[gunid] & 4)>0) //3 摇信超时
    {
     	 ui->can_state[gunid] = 3;//g_candata.errcode[gunid];
    }
    else
    {
    	 ui->can_state[gunid] = 0; //0  正常
	}
   // printf("打印电表故障状态  meter_err_state[gunid]=%d  gunid=%d\n",meter_err_state[gunid], gunid);
	if(g_candata.errcode[gunid]==1 )
	{
		printf("发生can故障！！！！！！gunid=%d\r\n", gunid);
		if(last_can_state[gunid]==0)
		{
			printf("记录发生can故障！！！！！！\r\n");
			writeErrToDb(gunid,100);
		}
	}
	last_can_state[gunid] = ui->can_state[gunid];
    
    // printf("打印电表故障状态  meter_err_state[gunid]=%d  gunid=%d\n",meter_err_state[gunid], gunid);
	if(meter_err_state[gunid]==1 && last_meter_state[gunid]==0)
	{
		printf("发生电表故障！！！！！！\r\n");
		writeErrToDb(gunid,101);
	}
	last_meter_state[gunid]=meter_err_state[gunid];

    // 枪号顺序标识  
    ui->gunmsg[gunid].guntab = gunid;
	if(g_candata.errcode[gunid]==0 )
	{
        errcode = get_gun_err_new(gunid,err_code);  // 生成告警表(告警数量，告警值表) 
	}
	else
	{
		errcode = 0;
	}
    
    // 停止充电原因，详细原因 
	g_uidata->gunmsg[gunid].stopreason   = g_stop_reason[gunid];
	g_uidata->gunmsg[gunid].m_stopReason = g_stop_detail[gunid];


	if(g_ifApp[gunid]==_APP_WAIT_STOP_)
	{
		g_sss[gunid]++;
		if(g_sss[gunid]>5 || g_uidata->cmd2[gunid].result == 2)// ||
		{
			g_ifApp[gunid]=_IDEL_MODE_APP_;
			g_uidata->cmd2[gunid].result =0;
			// printf("APP set _SYS_STOP_FIN_\r\n");
			gm_sys_state[gunid] =_SYS_STOP_FIN_;

			g_uidata->cmd3[gunid].state=g_ifApp[gunid];
		//	ui->gunmsg[gunid].charge_state = 1;//1;
		}
	}
	else if(g_ifApp[gunid]==_CARD_WAIT_STOP_)
	{
		     if(gm_sys_state[gunid]==_SYS_START_FAIL_)
			 {
				 g_sss[gunid]++;
				 if(g_sss[gunid]>=25)
				 {
					 gm_sys_state[gunid] =_SYS_STOP_FIN_;
					 g_ifApp[gunid]=_IDEL_MODE_CARD_;
				 }
			 }
		     else
		    	 g_ifApp[gunid]=_IDEL_MODE_CARD_;
	}
	else
		  g_uidata->cmd3[gunid].state=g_ifApp[gunid];
//	if (g_candata.errcode[gunid]) {
//
//		ui_debug1("出现can故障,停止充电！！！");
//		if (meter_err_state_xj[i]==1 && (gm_sys_state[i]==_SYS_START_FIN_ || gm_sys_state[i]==_SYS_WAIT_CHARGING_START_ ) )
//
//		TCUStopChargeProcess(gunid,5);
//	}
    charge_state[gunid]=getChargeState(gunid);

    printf("\r\n\r\n充电桩当前状态charge_state[%d]=%d\r\n\r\n ",gunid,charge_state[gunid]);
	if (gm_sys_state[gunid] == _SYS_READY_ )
	{
		ui->gunmsg[gunid].charge_state = 0;
	}
	else if (gm_sys_state[gunid] == _SYS_WAIT_CHARGING_START_)//0-1
	{
		ui->gunmsg[gunid].charge_state = 1;
		if (pconfig->devtype==AC)
			ui->gunmsg[gunid].charge_state = 3;
	}
	else if(gm_sys_state[gunid] ==_SYS_START_FIN_) // || (_))/c
	{
		ui->gunmsg[gunid].charge_state = 1;
	}
	else if(gm_sys_state[gunid] ==_SYS_STOP_FIN_)//gm_sys_state[gunid] ==_SYS_START_FAIL_ ||
	{
		if (ui->gunmsg[gunid].charge_state !=2)// &&  ui->gunmsg[gunid].charge_state !=4 && g_sss[gunid]>=4)
		   ui->gunmsg[gunid].charge_state =4;
	}
//	else if(gm_sys_state[gunid]==_SYS_START_FAIL_)
//		 {
//			 g_sss[gunid]++;
//			 if(g_sss[gunid]>=25)
//			 {
//				 gm_sys_state[gunid] =_SYS_STOP_FIN_;
//
//			 }
//		 }

	if(gm_sys_state[gunid] ==_SYS_STOP_FIN_ && charge_state[gunid]==0)
	{
		gm_sys_state[gunid] =_SYS_READY_;
	}

	if (charge_state[gunid] == 2)
	{
		if(gm_sys_state[gunid] == _SYS_START_FIN_)// || gm_sys_state[gunid] ==_SYS_WAIT_STOP_ || gm_sys_state[gunid] == _SYS_WAIT_CHARGING_START_)
		{
			 g_sss[gunid]++;
			 if(g_sss[gunid]>15)
			 {
				 gm_sys_state[gunid] =_SYS_STOP_FIN_;
				 // printf("TIME out set _SYS_STOP_FIN_\r\n");
				 ui->gunmsg[gunid].charge_state =2;
				 if(gm_sys_state[gunid] == _SYS_WAIT_CHARGING_START_ )
				 {
					 g_stop_reason[gunid]=5;
					 g_stop_detail[gunid]=CHARGE_STOP_UNEXP;
				 }
				 else
				 {
					 g_stop_reason[gunid]=5;
					 g_stop_detail[gunid]=CHARGE_STOP_UNEXP;

				 }
				 g_uidata->gunmsg[gunid].stopreason=g_stop_reason[gunid];
				 g_uidata->gunmsg[gunid].m_stopReason=g_stop_detail[gunid];
			 }

		}
		else
		{
			g_sss[gunid]++;
			if(g_sss[gunid]>15)
		 	{
			    gm_sys_state[gunid] =_SYS_STOP_FIN_;
				//printf("AUTO set _SYS_STOP_FIN_\r\n");
			    ui->gunmsg[gunid].charge_state =2;
			}
		}
	}

 	ui->gunmsg[gunid].gun_conn_state=get_gun_conn_state(gunid);
 	printf("获得充电枪[%d]连接状态 charge_state = %d\r\n", gunid, ui->gunmsg[gunid].charge_state);


#ifdef OLD_UI

  	if(gm_sys_state[gunid]==_SYS_WAIT_CHARGING_START_ || gm_sys_state[gunid]==_SYS_START_FIN_  || gm_sys_state[gunid]==_SYS_WAIT_STOP_)
 	{

  		ui->gunmsg[gunid].errcode=0;
 		g_uidata->gunmsg[gunid].meter_state=0;
 	}
  	else {
  		if(err_code[0]==0)
  			ui->gunmsg[gunid].errcode=0;
  		else
  			ui->gunmsg[gunid].errcode=err_code[1];
		g_uidata->gunmsg[gunid].meter_state = meter_err_state[gunid];
	//	printf("打印电表故障状态   g_uidata->gunmsg[gunid].meter_state =%d   meter_err_state[gunid]=%d  gunid=%d\n",g_uidata->gunmsg[gunid].meter_state ,meter_err_state[gunid], gunid);
		if (meter_err_state[gunid] == 1 && errcode == 0
				&& gm_sys_state[gunid] == _SYS_STOP_FIN_)
		{
	    	ui->gunmsg[gunid].errcode=101;
		}

	}
#endif
#ifndef OLD_UI

  	if(gm_sys_state[gunid]==_SYS_WAIT_CHARGING_START_ || gm_sys_state[gunid]==_SYS_START_FIN_  || gm_sys_state[gunid]==_SYS_WAIT_STOP_)
 	{
 		memset(ui->gunmsg[gunid].errcode,0,11);
 		g_uidata->gunmsg[gunid].meter_state=0;
 	}
  	else {
	   memcpy(ui->gunmsg[gunid].errcode,err_code,11);
		g_uidata->gunmsg[gunid].meter_state = meter_err_state[gunid];
	//	printf("打印电表故障状态   g_uidata->gunmsg[gunid].meter_state =%d   meter_err_state[gunid]=%d  gunid=%d\n",g_uidata->gunmsg[gunid].meter_state ,meter_err_state[gunid], gunid);
		if (meter_err_state[gunid] == 1 && errcode == 0
				&& gm_sys_state[gunid] == _SYS_STOP_FIN_)
		{
		    ui->gunmsg[gunid].errcode[0] = 1;
	     	ui->gunmsg[gunid].errcode[1] = 101;

		}

	}

#endif

     last_charge_state[gunid]=charge_state[gunid];
     ui->gunmsg[gunid].u2 =g_candata.gun[gunid].yc_data;//遥测
     //printf("gunid====%d ui-lll===%d %d  %d",gunid,ui->gunmsg[gunid].u2.dc.MX[0],ui->gunmsg[gunid].u2.dc.MX[1],ui->gunmsg[gunid].u2.dc.MX[2]);
#ifdef XJ_MQTT_CLIENT
      ui->gunmsg[gunid].u2.dc.MX[2]=g_mtdata_xj[gunid].end_soc;
#endif

#ifdef NW_CLIENT
     ui->gunmsg[gunid].u2.dc.MX[2]=g_mtdata[gunid].soc;
#endif

#ifdef  QL_CLOUD_CLIENT
     ui->gunmsg[gunid].u2.dc.MX[2]=g_mtdata_ql[gunid].end_soc;
#endif

 	return 0;
}



int ctrlpw_type;
char ShowStopReason;
char iflog;//是否将协议日志记录到flash中
char hardware_ver;//0 通用  1 210  2 600 3 700
char psam[16];


#ifdef OLD_CONF



void saveval(pconf1 *ppconf,int i,int val,char* strval)
{
	TDateTime tm_now;
	read_current_datetime(&tm_now);

	if (strcmp(conf_tab[i].name, "firmid") == 0) {//0
		ppconf->firm_id = val;
	} else if (strcmp(conf_tab[i].name, "devtype") == 0) {//1
		ppconf->devtype = val;
	} else if (strcmp(conf_tab[i].name, "gunnum") == 0) {//2
		ppconf->gunnum = val;
	} else if (strcmp(conf_tab[i].name, "devNO") == 0) {//3
		sprintf(ppconf->devNO, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "server_ip") == 0) {//4
		sprintf(ppconf->server_ip, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "server_port") == 0) {//5
		ppconf->server_port = val;
	} else if (strcmp(conf_tab[i].name, "1st_mt_comno") == 0) {//6
		ppconf->FMt_comno = val;
	} else if (strcmp(conf_tab[i].name, "1st_mt_baud") == 0) {//7
		ppconf->FMt_baud = val;
	} else if (strcmp(conf_tab[i].name, "phase_num") == 0) {//8
		if (ppconf->devtype == DC)
			ppconf->phase_num = 1;
		else if (ppconf->devtype == AC) {
			printf("电能表的相位数  phase_num=%d\r\n", val);
			ppconf->phase_num = val;
		}
	} else if (strcmp(conf_tab[i].name, "can_ver") == 0) {//9
		sprintf(ppconf->can_ver, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "propertyCode") == 0) {//10
		sprintf(ppconf->propertyCode, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "MaxV") == 0) {//11
		ppconf->MaxV = val;
	} else if (strcmp(conf_tab[i].name, "MaxV") == 0) {//12
		ppconf->MaxI = val;
	} else if (strcmp(conf_tab[i].name, "cannum") == 0) {//13
		ppconf->cannum = val;
	} else if (strcmp(conf_tab[i].name, "mt_portnum") == 0) {//14
		ppconf->mt_portnum = val;
	}
	else if (strcmp(conf_tab[i].name, "gunno_1st") == 0) {//15
		ppconf->gunno_1st = val;
	} else if (strcmp(conf_tab[i].name, "gunno_2nt") == 0) {//16
		ppconf->gunno_2nt = val;
	} else if (strcmp(conf_tab[i].name, "canaddr1") == 0) {//17
		ppconf->canaddr1 = val;
	} else if (strcmp(conf_tab[i].name, "canaddr2") == 0) {//18
		ppconf->canaddr2 = val;
	} else if (strcmp(conf_tab[i].name, "2nd_mt_comno") == 0) {//19
		ppconf->SMt_comno = val;
	} else if (strcmp(conf_tab[i].name, "1ST_METER_ADDR") == 0) {//20
		sprintf(ppconf->FMt_Addr, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "2ND_METER_ADDR") == 0) {//21
		sprintf(ppconf->SMt_Addr, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "ctrlpw_type") == 0) {//22
		ppconf->ctrlpw_type = val;
	} else if (strcmp(conf_tab[i].name, "ShowStopReason") == 0) {//23
		ppconf->ShowStopReason=val;
	}else if (strcmp(conf_tab[i].name, "LOGENABLE") == 0) {//24
		printf("tcu是否支持将协议日志写入flash  =%d\r\n ",val);//
		ppconf->iflog=val;
	}else if (strcmp(conf_tab[i].name, "HARDWARE_VER") == 0) {//25
		printf("tcu硬件版本号  =%d\r\n ",val);//
		ppconf->hardware_ver=val;
	}
#ifdef XJ_MQTT_CLIENT
	else if (strcmp(conf_tab[i].name, "MT_CT") == 0) {//26
		ppconf->mt_ct=val;
		printf("互感器变比  =%d  conf->mt_ct=%d\r\n ",val,ppconf->mt_ct);
	}
#endif
	else if (strcmp(conf_tab[i].name, "START_LOG") == 0) {//27
		char strx[7];
		char b1,b2;

		sprintf(strx, "%s\n", strval);
		b1=strx[4]-'0';
		b2=strx[5]-'0';
		ppconf->StartLogDay.Day=(b1*10)+b2;

		b1=strx[2]-'0';
		b2=strx[3]-'0';
		ppconf->StartLogDay.Month=(b1*10)+b2;
		b1=strx[0]-'0';
		b2=strx[1]-'0';
		ppconf->StartLogDay.Year=2000+b1*10+b2;

		ppconf->StartLogDay.Hour=0;
		ppconf->StartLogDay.Min=0;
		ppconf->StartLogDay.Sec=0;

		//if(cmp_DateTime(&tm_now,&pconfig->EndLogDay)>0)
		printf("日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);
	}else if (strcmp(conf_tab[i].name, "LOG_DAYS") == 0) {//28
		printf("333日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);
		ppconf->logdays=val;
		printf("444日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
		printf("日志开始记录天数=%d\n",ppconf->logdays);
	} else if (strcmp(conf_tab[i].name, "MAIN_VER") == 0) {//29
		printf("5555日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
		sprintf(ppconf->main_ver, "%s\n", strval);
		printf("tcu主控版本号  =%s\r\n ",ppconf->main_ver);//tcu主控版本号
		printf("555日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
	}
}

#else

void saveval(pconf *ppconf, int i, int val, char* strval)
{
	TDateTime tm_now;
	read_current_datetime(&tm_now);

	if (strcmp(conf_tab[i].name, "firmid") == 0) {//0
		ppconf->firm_id = val;
	} else if (strcmp(conf_tab[i].name, "devtype") == 0) {//1
		ppconf->devtype = val;
	} else if (strcmp(conf_tab[i].name, "gunnum") == 0) {//2
		ppconf->gunnum = val;
	} else if (strcmp(conf_tab[i].name, "server_ip") == 0) {//4
		sprintf(ppconf->server_ip, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "server_port") == 0) {//5
		ppconf->server_port = val;
	} else if (strcmp(conf_tab[i].name, "devNO") == 0) {//3
		sprintf(ppconf->devNO, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "propertyCode") == 0) {//10
		sprintf(ppconf->propertyCode, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "can_ver") == 0) {//9
		sprintf(ppconf->can_ver, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "MaxV") == 0) {//11
		ppconf->MaxV = val;
	} else if (strcmp(conf_tab[i].name, "MaxV") == 0) {//12
		ppconf->MaxI = val;
	} else if (strcmp(conf_tab[i].name, "InMt_Baud") == 0) { //12
		ppconf->MaxI = val;
		printf("输入电表的波特率=%d\n", val);
		ppconf->INMT_baud = val;
	} else if (strcmp(conf_tab[i].name, "In_phase_num") == 0) { //12
		ppconf->INMT_phase_num=val;
	} else if (strcmp(conf_tab[i].name, "OutMt_Baud") == 0) { //12
		ppconf->OUTMT_baud=val;
		printf("输出电表的波特率=%d\n",val);
	} else if (strcmp(conf_tab[i].name, "cannum") == 0) {//13
		ppconf->cannum = val;
	} else if (strcmp(conf_tab[i].name, "mt_portnum") == 0) {//14
		ppconf->mt_portnum = val;
	} else if (strcmp(conf_tab[i].name, "ctrlpw_type") == 0) {//22
		printf("充电桩是否支持共充  conf->ctrlpw_type=%d\r\n ",val);
		ppconf->ctrlpw_type = val;
	} else if (strcmp(conf_tab[i].name, "ShowStopReason") == 0) {//23
		ppconf->ShowStopReason=val;
	}else if (strcmp(conf_tab[i].name, "LOGENABLE") == 0) {//24
		printf("tcu是否支持将协议日志写入flash  =%d\r\n ",val);//
		ppconf->iflog=val;
	}else if (strcmp(conf_tab[i].name, "HARDWARE_VER") == 0) {//25
		printf("tcu硬件版本号  =%d\r\n ",val);//
		ppconf->hardware_ver=val;
	}
#ifdef XJ_MQTT_CLIENT
	else if (strcmp(conf_tab[i].name, "MT_CT") == 0) {//26
		ppconf->mt_ct=val;
		printf("互感器变比  =%d  conf->mt_ct=%d\r\n ",val,ppconf->mt_ct);
	}
#endif
	else if (strcmp(conf_tab[i].name, "START_LOG") == 0) {//27
		char strx[7];
		char b1,b2;

		sprintf(strx, "%s\n", strval);
		b1=strx[4]-'0';
		b2=strx[5]-'0';
		ppconf->StartLogDay.Day=(b1*10)+b2;

		b1=strx[2]-'0';
		b2=strx[3]-'0';
		ppconf->StartLogDay.Month=(b1*10)+b2;
		b1=strx[0]-'0';
		b2=strx[1]-'0';
		ppconf->StartLogDay.Year=2000+b1*10+b2;

		ppconf->StartLogDay.Hour=0;
		ppconf->StartLogDay.Min=0;
		ppconf->StartLogDay.Sec=0;

		//if(cmp_DateTime(&tm_now,&pconfig->EndLogDay)>0)
		printf("日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);
	}else if (strcmp(conf_tab[i].name, "LOG_DAYS") == 0) {//28
		printf("333日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);
		ppconf->logdays=val;
		printf("444日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
		printf("日志开始记录天数=%d\n",ppconf->logdays);
	} else if (strcmp(conf_tab[i].name, "MAIN_VER") == 0) {//29

		printf("5555日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
		sprintf(ppconf->main_ver, "%s\n", strval);
		printf("tcu主控版本号  =%s\r\n ",ppconf->main_ver);//tcu主控版本号
		printf("555日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day,ppconf->logdays);
	} else if (strcmp(conf_tab[i].name, "INTERFACE_ID") == 0) {
        sprintf(ppconf->interface_id, "%s", strval);
	} else if (strcmp(conf_tab[i].name, "TOKEN") == 0) {
        sprintf(ppconf->token, "%s", strval);
	}
}

//void matchkey(pconf *conf, char* str) {
//	int num;
//	int i;
//	char *p = NULL;
//	int val;
//	char strval[32];
//	int match_flag = 0;
//
//	num=ARRAY_LEN(conf_tab);
//	main_debug("读出的字符串：%s",str);
//	for (i = 0; i < num; i++) {
//		p = strstr(str, conf_tab[i].name); //如can_num=
//		if (p) {
//			p = strstr(str, "=");
//			if (conf_tab[i].typeVal == TY_INT) {
//				if (str[strlen(str) - 2] == '\r')
//					str[strlen(str) - 2] = '\0';
//				else if (str[strlen(str) - 1] == '\r')
//					str[strlen(str) - 1] = '\0';
//				val = atoi(p + 1);
//				match_flag = 1;
//			} else if (conf_tab[i].typeVal == TY_STR) {
//				sprintf(strval, "%s", p+1);
//				match_flag = 1;
//			} else
//				match_flag = 0;
//
//			if (match_flag == 0)
//				continue;
//			switch (i) {
//			case 0:
//				conf->firm_id = val;
//				break;
//			case 1:
//				conf->devtype = val;
//				break;
//			case 2:
//				conf->gunnum = val;
//				break;
//
//			case 3:
//				sprintf(conf->server_ip, "%s", strval);
//				break;
//			case 4:
//				conf->server_port = val;
//				break;
//			case 5:
//				sprintf(conf->devNO, "%s", strval);
//				break;
//			case 6:
//				sprintf(conf->propertyCode, "%s", strval);
//				break;
//
//			case 7:
//				sprintf(conf->can_ver, "%s", strval);
//				break;
//			case 8:
//				conf->MaxV = val;
//				break;
//			case 9:
//				conf->MaxI = val;
//				break;
//			case 10:
//				printf("输入电表的波特率=%d\n",val);
//				conf->INMT_baud=val;
//				break;
//			case 11:
//				conf->INMT_phase_num=val;
//				break;
//			case 12:
//				printf("输出电表的波特率=%d\n",val);
//				conf->OUTMT_baud=val;
//				break;
//			case 13:
//				conf->cannum = val;
//		     break;
//			case 14:
//				conf->mt_portnum = val;
//		     break;
//			case 15:
//				printf("充电桩是否支持共充  conf->ctrlpw_type=%d\r\n ",val);
//				conf->ctrlpw_type = val;
//				break;
//			case 16:
//				conf->ShowStopReason=val;
//				break;
//			case 17:
//				printf("tcu是否支持将协议日志写入flash  =%d\r\n ",val);//
//				conf->iflog=val;
//				break;
//			case 18:
//				printf("tcu硬件版本号  =%d\r\n ",val);//
//				conf->hardware_ver=val;
//				break;
//			case 19:
//
//				sprintf(conf->psam, "%s", strval);
//				printf("读卡器PSAM  =%s\r\n ",conf->psam);
//				break;
//			case 20:
//
//				sprintf(conf->main_ver, "%s\n", strval);
//				printf("tcu主控版本号  =%s\r\n ",conf->main_ver);//tcu主控版本号
//
//				break;
//
//			default:
//				break;
//			}
//
//		}
//
//	}
//}

#endif

int matchkey1(void * para, char* str) {
	int num;
	int i;
	char *p = NULL;
	int val;
	char strval[64];
	int match_flag = 0;

	num=ARRAY_LEN(conf_tab);
	main_debug("读出的字符串：%s",str);
	for (i = 0; i < num; i++) {
//	    main_debug("读出的字符串：%s  %s",str,conf_tab[i].name);
		p = strstr(str, conf_tab[i].name);  // 检索是否是对应的配置项 
		if (p) {    // 当前处理项与配置项匹配时，进行处理 
			p = strstr(str, "=");
			strcpy(conf_tab[i].val,p+1);   // 将配置文件中的内容，拷贝到 内存配置信息中 
			conf_tab[i].val[strlen(p+1)-1]=0x00;
			main_debug("读出的字符串(配置)：%s len=%d",conf_tab[i].val, strlen(p+1));
			if (conf_tab[i].typeVal == TY_INT) {    // 内容为数值时处理方法 
                // 针对windows下和linux下换行符进行差异处理 
				if (str[strlen(str) - 2] == '\r')
					str[strlen(str) - 2] = '\0';
				else if (str[strlen(str) - 1] == '\r')
					str[strlen(str) - 1] = '\0';
				val = atoi(p + 1);
				match_flag = 1;
			} else if (conf_tab[i].typeVal == TY_STR) {
				sprintf(strval, "%s", p+1);
				match_flag = 1;
			} else {
				match_flag = 0;
			}
            // 只处理匹配的数据 
			if (match_flag == 0)
				continue;

#ifdef OLD_CONF
			saveval((pconf1 *)para, i, val,strval);
#else
			saveval((pconf *)para, i, val,strval);
#endif
			main_debug("字符串匹配成功：i=%d",i);
			flag_config &= ~(1<<i);
            return i;
		}
	}
	main_debug("字符串匹配不成功：i=%d",i);
	return -1;
}

void matchkey2(void* para)
{
	int num;
	int val;
	char strval[64];
	int i;

	num=ARRAY_LEN(conf_tab);
    for(i=0;i<num;i++)
    {
           if(!flag_config&(1<<i))
                continue;

           if(conf_tab[i].typeVal == TY_INT)
            {
           		val = atoi(conf_tab[i].val);
            }
            else if(conf_tab[i].typeVal == TY_STR)
            {
           		sprintf(strval, "%s",conf_tab[i].val);
            }
#ifdef OLD_CONF
			saveval((pconf1 *)para, i, val,strval);
#else
			saveval((pconf *)para, i, val,strval);
#endif


    }


}

int findLogId( char* str) {
	int num;
	int i;
	char *p = NULL;

	num=ARRAY_LEN(conf_tab);
	main_debug("读出的字符串：%s",str);
	for (i = 0; i < num; i++) {
		p = strstr(str,"LOGENABLE" ); //conf_tab[i].name
		if (p) {

            return i;
		}
//		else
//			break;

	}
	main_debug("字符串匹配不成功：i=%d",i);
	return -1;

}
int ModiLogMark(void)
{
	int ret=-1,ret1=-1;
    char temp_str[64];
    char* ptemp=NULL;
  //  char temp_str_last[64];
    int lenlen=0,lenlen_last;//,lenlen_last_last;
    int i=0;
    int flag=0;

	ret=createStrfile(FILE_ID_TCU_CONF);
	main_debug("ModiLogMark建立或打开配置文件  ret=%d %s",ret,FILE_NAME_TCU_CONF);
	if (ret == 0) {
		while (1) {

	//		main_debug("配置文件已经到尾部?????  %s  %s", temp_str_last, temp_str);
			ret1 = parse_config(FILE_ID_TCU_CONF, temp_str);

			if (ret1 != 0) {
				main_debug("配置文件已经到尾部！！！！");
				fs_close(FILE_ID_TCU_CONF);
				break;
			}
			lenlen_last = lenlen;
			ptemp = strstr(temp_str, "LOGENABLE"); //conf_tab[i].name
			if (ptemp != NULL) {
				main_debug("配置文件第%d条正确 ！！！！lenstr=%d  str=%s", i,
						sizeof(temp_str), temp_str);
				flag = 1;
				break;

			}
			lenlen += strlen(temp_str);
			i++;

		}
	}
	else
	{
		fs_close(FILE_ID_TCU_CONF);
		return -1;
	}
	fs_close(FILE_ID_TCU_CONF);

	if (flag == 1) {
		int rett = 0;
		fs_open(FILE_ID_TCU_CONF, "r+");
		fs_lseek(FILE_ID_TCU_CONF, lenlen_last, SEEK_SET);
		main_debug("写入配置文件第%d条,rett=%d", i, rett);
		sprintf(temp_str, "%s=%d\n", conf_tab[i].name, 0);
		main_debug("写入配置文件第%d条 %s", i, temp_str);
		rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		i++;
		main_debug("写入配置文件第%d条,rett=%d", i, rett);
		sprintf(temp_str, "%s=%02d%02d%02d\n", conf_tab[i].name,
				pconfig->StartLogDay.Year - 2000, pconfig->StartLogDay.Month, pconfig->StartLogDay.Day);
		main_debug("写入配置文件第%d条 %s", i, temp_str);
		rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		i++;
		main_debug("写入配置文件第%d条,rett=%d", i, rett);
		sprintf(temp_str, "%s=%s\n", conf_tab[i].name, MAIN_VER_NEW);
		main_debug("写入配置文件第%d条 %s", i, temp_str);
		rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		fs_close(FILE_ID_TCU_CONF);
		return 1;

	}
	return 0;
}

int ReadConf1(void *para)
{
	int ret=-1,ret1=-1,ret2=-1;
    int lenlen=0,lenlen_last,lenlen_last_last,lenlen_log;
    int i=0;
    char* p=NULL;
	TDateTime tm_now,EndLogDay;

    int num=ARRAY_LEN(conf_tab);
    int flag=0;
    u32 flag_config1;
    char temp_str[64];
    
    flag_config = 0;
	for(i=0;i<num;i++)
	{
		flag_config |= (1<<i);
	}
	flag_config1=flag_config;
    printf("配置文件的条目数据=%d  %x\n",num,(int)flag_config);
	i=0;
#ifdef OLD_CONF
	pconf1 *ppconf=(pconf1 *)para;
#else
	pconf *ppconf=(pconf *)para;
#endif
	ret=createStrfile(FILE_ID_TCU_CONF);
	main_debug("建立或打开配置文件  ret=%d",ret);
	if (ret == 0) {
		while (i<=num) {
            // 读取一行配置参数，模板参数  
			ret1 = parse_config(FILE_ID_TCU_CONF, temp_str);
			if (ret1 != 0) {
				main_debug("配置文件已经到尾部！！！！");      // 没有读到数据，即认为数据读取完毕 
				break;
			}
			if (i == 1)
			{
				lenlen_last = lenlen;
			}
			else if (i >= 2)
            {
				lenlen_last_last = lenlen_last;
				lenlen_last = lenlen;
				p = strstr(temp_str, "LOGENABLE");
				if (p!=NULL)
				{
				    lenlen_log=lenlen;
				    printf("日志记录标志id=%d\n",i);
				}
            }
			lenlen += strlen(temp_str);
			main_debug("配置文件第%d条正确 ！！！！lenstr=%d  str=%s  ", i, sizeof(temp_str), temp_str);
			i++;
			ret1 = matchkey1(para, temp_str);
			if(ret1!=(i-1) || ret1==-1)
				flag = 1;

			printf("配置文件变化情况    flag_config=%x ret1=%d  flag=%d\n",
					(int) flag_config, ret1, flag);
		}
		printf("666日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);

		if (flag_config == 0) {

			printf("tcu主控版本号  =%s   lenlen=%d\r \n ", ppconf->main_ver, lenlen); //

			printf("000日志开始记录日期  year=%d  month=%d  day=%d\n",ppconf->StartLogDay.Year,ppconf->StartLogDay.Month,ppconf->StartLogDay.Day);
			if (memcmp(ppconf->main_ver, MAIN_VER_NEW, 6)) {
				printf("tcu主控版本号好像变了  =%s %s %d %s lenlen=%d %d %d\r\n ",
						ppconf->main_ver, MAIN_VER_NEW, strlen(temp_str),
						temp_str, lenlen,lenlen_last,lenlen_last_last); //
				memcpy(ppconf->main_ver, MAIN_VER_NEW, 6);

				ret2 = 3;
			}
			fs_close(FILE_ID_TCU_CONF);
		} else {

			matchkey2(para );
			ret2=0;
			fs_close(FILE_ID_TCU_CONF);
			printf("文件需要重写11111！！！！！\n");
            flag=1;
		}
	}
	else
	{

		main_debug("配置文件不存在，建立成功！！！！！num=%d\r\n",num);
		for(i=0;i<num-1;i++)
		{
			sprintf(temp_str, "%s=%s\n", conf_tab[i].name,conf_tab[i].val);
			fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		}
		sprintf(temp_str, "%s=%s\n", conf_tab[i].name,MAIN_VER_NEW);
		fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));

		fs_close(FILE_ID_TCU_CONF);
		ret2=0;
		return 1;
	}

	  read_current_datetime(&tm_now);
	   if(ret2==3)
	   {
		   printf("u1111111111111111111111\n");
			main_debug("配置文件版本号已经发生变化，！！！！！num=%d \r\n",num);//输入电表的波特率
			i=num-3;
			int rett = 0;
				fs_open(FILE_ID_TCU_CONF, "r+");
				fs_lseek(FILE_ID_TCU_CONF, lenlen_log, SEEK_SET);
				main_debug("写入配置文件第%d条,rett=%d", i, rett);
				sprintf(temp_str, "%s=%d\n", conf_tab[i].name, 1);
				main_debug("写入配置文件第%d条 %s", i, temp_str);
				rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
				i++;
				main_debug("写入配置文件第%d条,rett=%d", i, rett);
				sprintf(temp_str, "%s=%02d%02d%02d\n", conf_tab[i].name,
						tm_now.Year - 2000, tm_now.Month, tm_now.Day);
				main_debug("写入配置文件第%d条 %s", i, temp_str);
				rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
				i++;
				main_debug("写入配置文件第%d条,rett=%d", i, rett);
				sprintf(temp_str, "%s=%s\n", conf_tab[i].name, MAIN_VER_NEW);
				main_debug("写入配置文件第%d条 %s", i, temp_str);
				rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
				fs_close(FILE_ID_TCU_CONF);
				return 1;

	   }
	   else if(ppconf->iflog==1 && flag!=1 )
	   {
		   printf("u2222222222222222222222222\n");
			main_debug("需要日志！！！！！num=%d \r\n",num);
			EndLogDay=pconfig->StartLogDay;
			add_DateTime_Day(&EndLogDay,pconfig->logdays);
			printf("日志开始记录日期  year=%d  month=%d  day=%d  天数=%d\n",EndLogDay.Year,EndLogDay.Month,EndLogDay.Day,pconfig->logdays);
			printf("日志结束记录日期  year=%d  month=%d  day=%d\n",EndLogDay.Year,EndLogDay.Month,EndLogDay.Day);

			if(cmp_DateTime(&tm_now,&EndLogDay)>0)
			{
				  printf("日志超过记录日期\n");
						int rett = 0;
						i=num-3;
						fs_open(FILE_ID_TCU_CONF, "r+");
						fs_lseek(FILE_ID_TCU_CONF, lenlen_log, SEEK_SET);
						main_debug("写入配置文件第%d条,rett=%d", i, rett);
						sprintf(temp_str, "%s=%d\n", conf_tab[i].name, 1);
						main_debug("写入配置文件第%d条 %s", i, temp_str);
						rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
						i++;
						main_debug("写入配置文件第%d条,rett=%d", i, rett);
						sprintf(temp_str, "%s=%02d%02d%02d\n", conf_tab[i].name,
								tm_now.Year - 2000, tm_now.Month, tm_now.Day);
						main_debug("写入配置文件第%d条 %s", i, temp_str);
						rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
						i++;
						main_debug("写入配置文件第%d条,rett=%d", i, rett);
						sprintf(temp_str, "%s=%s\n", conf_tab[i].name, MAIN_VER_NEW);
						main_debug("写入配置文件第%d条 %s", i, temp_str);
						rett = fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
						fs_close(FILE_ID_TCU_CONF);
						return 1;

			}
	   }
//	   else
//		   printf("uuuuuuuuuuyyyyyyyyyynnnnnnnnnnnnnnnppconf->iflog=%d\n",ppconf->iflog);
	   printf("u3333333333333333333333333\n");
   if(flag==1)
   {
	   printf("u44444444444444444444444444444444\n");
	    fs_open(FILE_ID_TCU_CONF,"w+");

		main_debug("配置文件结构变化，重新写入！！！！！num=%d\r\n",num);
		for(i=0;i<num-1;i++)
		{
			sprintf(temp_str, "%s=%s\n", conf_tab[i].name,conf_tab[i].val);
			fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		}
		sprintf(temp_str, "%s=%s\n", conf_tab[i].name,MAIN_VER_NEW);
		fs_write(FILE_ID_TCU_CONF, temp_str, strlen(temp_str));
		fs_close(FILE_ID_TCU_CONF);
		ret2=0;
		return 1;
   }
	return 0;
}

int writecfg()
{
	int num;
	int i;
	num=ARRAY_LEN(conf_tab);
    fs_open(FILE_ID_TCU_CONF,"w+");
    
	for(i=0;i<num;i++)
	{
		
		
	}

    return 0;


}

void time_gun(u8 gunid,int interval)
{
	static int lastTime_sendRd[] = { 0, 0 ,0,0,0,0,0,0};
    u8 charge_state;
    charge_state=getChargeState(gunid);
	//msgNW  msg;

	if (lastTime_sendRd[gunid] != 0 && gm_sys_state[gunid] == _SYS_READY_) {
		lastTime_sendRd[gunid] = 0;
	}
	if (g_msg_save[gunid] == _MSG_SAVE_START_)
	{
		datarecording(gunid, 1);
		printf("充电开始保存充电记录 gunid=%d！！！\r\n",gunid);
     	SavechargeRecord(gunid,0,0);
		g_msg_save[gunid] = _MSG_NULL_;
		lastTime_sendRd[gunid] = 0;
	}
	else if (gm_sys_state[gunid] == _SYS_START_FIN_) {
		lastTime_sendRd[gunid] += interval;
		if (lastTime_sendRd[gunid] >= 30 || g_msg_save[gunid] == _MSG_SAVE_CHANGE_){
			SavechargeRecord(gunid, 1, 0);
			lastTime_sendRd[gunid] = 0;
			if(g_msg_save[gunid] == _MSG_SAVE_CHANGE_)
					g_msg_save[gunid] = _MSG_NULL_;
		}
	}


//	if (g_msg_save[gunid] == _MSG_SAVE_STOP_ && charge_state==0) {
////
////		printf("充电结束保存充电记录！！！ g_stop_reason[%d]=%d\r\n",gunid,g_stop_reason[gunid]);
////		SavechargeRecord(gunid, 2, g_stop_reason[gunid]);第二次
//		g_msg_save[gunid] = _MSG_NULL_;
//		return;
//
//	}


}
void registerdb(void)
{

   db_register db;
	db.db_card_type=0;
	db.db_err_type=0;

#ifdef NW_CLIENT
	int i;
	db.db_tran_type = TABLE_NW;

	for (i = 0; i < pconfig->gunnum; i++) {
		g_mtdata[i].soc = 0;
	}

#endif

#ifdef YCC_CLIENT
	db.db_tran_type = TABLE_YCT;
#endif

#ifdef XJ_MQTT_CLIENT

	db.db_tran_type=TABLE_XJ;
#endif

#ifdef QL_CLOUD_CLIENT
    db.db_tran_type = TABLE_QL;
#endif

#ifdef QL_CLOUD_CLIENT

#if GUN_NUM == 8
	  int i;
	u32 offset1,offset2;
	Meter_Info* ptr1;
	Gun_Info* ptr2;

	 Gun_Info info;
	for(i=0;i<pconfig->gunnum;i++)
	{
		printf("registerdb4444444444444444pconfig->gunnum=%d\r\n", pconfig->gunnum);
		if (protocol_get_GunInfo(i, &info) == 0) {
			gun_can_info_tab[i].port = info.can_port;
			gun_can_info_tab[i].frm_gunno = info.frame_gunno;
			gun_can_info_tab[i].cha_addr = info.cha_addr;
//
			printf("枪的信息  port=%d   gunno=%d   cha_addr=%x  tcu_addr=%x\n",
			gun_can_info_tab[i].port, gun_can_info_tab[i].frm_gunno,
			gun_can_info_tab[i].cha_addr, gun_can_info_tab[i].tcu_addr);
		   meter_info_tab[i].ct=info.ct;
	       meter_info_tab[i].phase_num=info.phase_num;
		   meter_info_tab[i].portid=info.mt_port;
		   offset1=OFFSET_OF(Meter_Info,meteraddr);
           offset2=OFFSET_OF(Gun_Info,meteraddr);
           ptr1=&meter_info_tab[i];
           ptr2=&info;
		   memcpy((u8*)((u32)&meter_info_tab[i]+offset1),(u8*)((u32)&info+offset2),6);
			usleep(100);

		   debug_meter1("枪的表地址 i=%d  addr=%.*a %.*a  端口号=%d",i,6,(u8*)((u32)&info+offset2),6,(u8*)((u32)&meter_info_tab[i]+offset1),meter_info_tab[i].portid);
		}

		else
		{
		   printf("没有得到正确的表号！！！！\n");
			sleep(2);
		}
//个枪的端口号=

	}
#endif  // GUN_NUM == 8
#ifdef OLD_CONF
		int    mt_ct;
        printf("打印pconfig->mt_ct=%d   \n",pconfig->mt_ct);

			switch(pconfig->mt_ct)
			{
			case 1:
				mt_ct=1;
				break;
			case 2:
				mt_ct=30;
				break;
			case 3:
				mt_ct=40;
				break;
			case 4:
				mt_ct=50;
				break;
			case 5:
				mt_ct=60;
				break;
			case 6:
				mt_ct=80;
				break;
			case 10:
				mt_ct=10;
				break;
			default:
				mt_ct=1;
			}
	for (i = 0; i < pconfig->gunnum; i++) {

		meter_info_tab[i].ct = 1000 * mt_ct;

        printf("打印ct=%d  %d   \n",mt_ct,meter_info_tab[i].ct);
    	sprintf(_tmp_print_str,"gunid=%d 互感器倍率 CT =%d\n",i,mt_ct);
    	fs_debug_104(_tmp_print_str);
	}
#endif//OLD_CONF
#endif// QL_CLOUD_CLIENT

	protocol_database_register(&db);
    printf("registerdb1111111111111111111111pconfig->gunnum=%d\r\n",pconfig->gunnum);
}

void addtime_main_1s(int interval)
{
	int i;
    
	for (i = 0; i < pconfig->gunnum; i++)
	{
		time_gun(i,interval);
	}
}
//int initQRCode(void )
//{
//
//	int count;
//	int start;
//	int fid;
//	int i;
//	char devNo[32];
//	fid = FILE_ID_QRCODE;
//	start = QRCODE_START;
////	count = app_fs_read(fid, start, QRCODESIZE, (u8*) g_uidata->cmd6[0].qrCode);
////	if (count == 0) {
//
//	memcpy(devNo,conf.devNO,strlen(pconfig->devNO));
//	for(i=0;i<32;i++)
//	{
//		if(devNo[i]==0x0d ||devNo[i]==0x0a)
//			devNo[i]=0x00;
//	}
//		printf("设置1号枪！！！！！！！！！！！！！！！！！！\r\n");
////		sprintf(g_uidata->cmd6[0].qrCode, "%s",
////				"hlht://TEST0004301.101437000/");
//
//
//		sprintf(g_uidata->cmd6[0].qrCode, "hlht://%s01.101437000/",devNo);
//		count = app_fs_write(fid, start, QRCODESIZE,
//				(u8*) g_uidata->cmd6[0].qrCode);
//
//		start = QRCODE_START + QRCODESIZE;
//		printf("设置2号枪！！！！！！！！！！！！！！！！！！\r\n");
////		sprintf(g_uidata->cmd6[1].qrCode, "%s",
////				"hlht://TEST0004302.101437000/");
//		sprintf(g_uidata->cmd6[1].qrCode, "hlht://%s02.101437000/",devNo);
//
//		count = app_fs_write(fid, start, QRCODESIZE,
//				(u8*) g_uidata->cmd6[0].qrCode);
////	}
////	else
////	{
////		start = QRCODE_START + QRCODESIZE;
////		printf("读取1号枪 %s\n",g_uidata->cmd6[0].qrCode);
////		start = QRCODE_START + QRCODESIZE;
////		count = app_fs_read(fid, start, QRCODESIZE, (u8*) g_uidata->cmd6[1].qrCode);
////
////	}
//	return 0;
//
//}



